<template>
  <div>
    <el-container>
      <el-header style="margin-top: 10px;height: auto;">
        <el-row>
          <el-select
            v-model="search_query.favorite"
            placeholder="收藏"
            style="width: 100px"
            clearable
            @change="handleSearch"
          >
            <el-option
              label="所有"
              value=""
            />
            <el-option
              label="收藏"
              value="true"
            />
            <el-option
              label="未收藏"
              value="false"
            />
          </el-select>
          <el-select
            v-model="search_query.processed"
            placeholder="收藏"
            style="width: 100px"
            clearable
            @change="handleSearch"
          >
            <el-option
              label="所有"
              value=""
            />
            <el-option
              label="已处理"
              value="true"
            />
            <el-option
              label="未处理"
              value="false"
            />
          </el-select>
          <el-input
            v-model="search_query.kw"
            style="width: 50%"
            placeholder="输入 任意字符 进行筛选"
            class="input-with-select"
            clearable
            @keyup.native.enter="handleSearch"
          >
            <el-button slot="append" icon="el-icon-search" :loading="listLoading" @click="handleSearch">搜索</el-button>
          </el-input>
          <el-button
            type="primary"
            class="el-icon-plus add-btn"
            :disabled="listLoading"
            @click="handleAction('create', '添加')"
          >添加
          </el-button>
        </el-row>
      </el-header>
      <el-main>
        <el-table
          v-loading="listLoading"
          :data="data"
          border
          fit
          highlight-current-row
          style="width: 100%"
          @sort-change="handleSort"
        >
          <el-table-column align="center" label="电话" width="250" prop="phone" />
          <el-table-column align="center" label="描述" prop="description" sortable="custom" />
          <el-table-column align="center" label="平台:" width="150" prop="plantform" sortable="custom">
            <template slot-scope="scope">
              <a :href="scope.row.link" style="color: blue;" target="_blank">{{ scope.row.plantform }}</a>
            </template>
          </el-table-column>
          <el-table-column align="center" label="搜索词" width="200" prop="search_keyword" sortable="custom" />
          <el-table-column align="center" label="处理次数" width="150" prop="processCount" sortable="custom" />
          <el-table-column align="center" label="处理情况" width="150" prop="favorite" sortable="custom">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.processed"
                style="padding: 7px 15px;float: right"
                title="处理情况"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="handleSet(scope.row,'processed')"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="收藏" width="100" prop="favorite" sortable="custom">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.favorite"
                style="padding: 7px 15px;float: right"
                title="收藏"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="handleSet(scope.row,'favorite')"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200" fixed="right">
            <template slot-scope="scope">
              <el-button
                v-show="!scope.row.edit"
                size="mini"
                icon="el-icon-edit"
                type="warning"
                plain
                @click="handleAction('edit', scope.row)"
              >修改
              </el-button>
              <el-button
                v-show="!scope.row.edit"
                size="mini"
                icon="el-icon-delete"
                type="danger"
                plain
                @click="handleAction('delete', scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="data.length"
          background
          class="mt-pagination"
          :current-page="search_query.page"
          :page-sizes="[10,20,50,100]"
          :page-size="search_query.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="search_query.total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </el-main>
    </el-container>
    <el-dialog
      append-to-body
      :title="title"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="120px">
        <el-form-item label="name" prop="name">
          <el-input v-model="temp.phone" />
        </el-form-item>
        <el-form-item label="description" prop="description">
          <el-input v-model="temp.description" />
        </el-form-item>
        <el-form-item label="search_keyword" prop="search_keyword">
          <el-input v-model="temp.search_keyword" />
        </el-form-item>
        <el-form-item label="收藏" label-width="120px">
          <el-switch
            v-model="temp.favorite"
            style="padding: 7px 15px;float: right"
            active-text="收藏"
            inactive-text="取消"
            title="收藏"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/crm/crmm'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Whatsapp',
  data() {
    return {
      table_name: 'CRMWhatsapp',
      data: [],
      listLoading: false,
      kwInput: '',
      search_query: {
        kw: '',
        table_name: 'CRMWhatsapp',
        favorite: '',
        processed: 'false',
        order_by: '',
        order_desc: '',
        // pagination
        pagesize: 10,
        page: 1,
        total: 0
      },
      favorite: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      action: null,
      dialogVisible: false,
      title: '',
      inputVisible: false,
      inputValue: '',
      predefineColors: [
        '#6C6762',
        '#67C23A',
        '#F56C6C',
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#00ced1',
        '#1e90ff',
        '#c71585'
      ],
      temp: {
        phone: '',
        link: '',
        processCount: 0,
        search_keyword: '',
        description: '',
        favorite: false
      },
      width: '',
      innerDialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTemp() {
      this.temp = {
        phone: '',
        link: '',
        processCount: 0,
        search_keyword: '',
        description: '',
        favorite: false
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify(this.search_query))
        .then(response => {
          this.data = response.data.data
          this.listLoading = false
          this.search_query.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '640px'
          this.title = '添加'
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '640px'
          this.title = `确认要删除:${row.name}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '640px'
          this.title = `修改:${row.name}`
          Object.keys(this.temp).forEach(key => {
            if (row[key] !== undefined) this.temp[key] = row[key]
          })
          this.temp.id = row.id
          this.innerDialogVisible = true
          break
      }
    },
    handleSet(row, field) {
      Put(row.id, { [field]: row[field], 'table_name': this.table_name }).then((response) => {
        this.innerDialogVisible = false
        this.getList()
        this.notify('更新成功')
      })
    },
    confirm() {
      const _data = this.temp
      _data.table_name = this.table_name
      switch (this.action) {
        case 'create':
          Post(_data
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id, this.temp).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, _data).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.search_query.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.search_query.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.search_query.page = 1
      this.getList()
    },
    handleSort(v) {
      this.search_query.order_by = v.prop
      this.search_query.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    }
  }
}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}

.top >>> .el-select .el-input {
  width: 350px;
}

.top >>> .input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.top {
  padding-left: 20px;
  margin-bottom: 30px;
  margin-top: 28px;
}

.input-with-select {
  width: calc(100% - 200px);
}

.add-btn {
  float: right;
  margin-right: 20px;
  width: 150px
}

.mt-pagination {
  float: right;
  margin-top: 5px;
}

.app-container >>> .el-dialog {
  margin-top: 48px !important;
  margin-bottom: 68px;
}

.mt-dialog >>> .el-transfer-panel {
  width: calc(50% - 60px);
}

.mt-form >>> .el-select {
  width: 100%;
}

.mt-form >>> .el-input__inner {
  width: 100%;
}

.mt-dialog >>> .el-dialog {
  margin-top: 60px;
  /*margin: 168px auto !important;*/
}

.mt-dialog >>> .el-dialog__body {
  padding-bottom: 100px;
}
</style>
